﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace SHomeWorkshop.LunarMind.Dialogs
{
    /// <summary>
    /// Interaction logic for BaseTextEditor.xaml
    /// </summary>
    public partial class BaseTextEditor : Window
    {
        public BaseTextEditor(bool selectAllTextWhenLoaded = false)
        {
            InitializeComponent();
            inputTextBox.ContextMenu = null;
            inputTextBox.Focus();

            this.selectAllWhenLoaded = selectAllTextWhenLoaded;

            InputMethod.SetIsInputMethodEnabled(inputTextBox, true);

            //给边框添加变色效果，结果发现有打扰人的反面效果，于是改成只闪动两次。
            //Brush myBrush = new SolidColorBrush();

            //ColorAnimation myColorAnimation = new ColorAnimation();

            //myColorAnimation.From = Colors.Transparent;

            //myColorAnimation.To = Colors.DarkCyan;
            //myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(200));
            //myColorAnimation.AutoReverse = true;
            //myColorAnimation.RepeatBehavior = new RepeatBehavior(2);

            ////将动画应用到画刷（Brush）的“Color”属性。
            //myBrush.BeginAnimation(SolidColorBrush.ColorProperty, myColorAnimation);
            //outBorder.BorderBrush = myBrush;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            inputTextBox.SelectedText = (sender as Button).Content.ToString();
            inputTextBox.SelectionStart += 1;
            inputTextBox.SelectionLength = 0;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void inputTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            bool isShift = false;

            KeyboardDevice kd = e.KeyboardDevice;

            if ((kd.GetKeyStates(Key.LeftShift) & KeyStates.Down) > 0 ||
                (kd.GetKeyStates(Key.RightShift) & KeyStates.Down) > 0)
            {
                isShift = true;
            }

            bool isCtrl = false;

            if ((kd.GetKeyStates(Key.LeftCtrl) & KeyStates.Down) > 0 ||
                (kd.GetKeyStates(Key.RightCtrl) & KeyStates.Down) > 0)
            {
                isCtrl = true;
            }

            switch (e.Key)
            {
                case Key.Enter:
                    {
                        if (isShift == false)
                        {
                            if (isCtrl == false)
                            {
                                btnOk_Click(sender, e);
                            }
                        }
                        else
                        {
                            this.inputTextBox.SelectedText = "\r\n";
                            this.inputTextBox.SelectionLength = 0;
                            this.inputTextBox.SelectionStart += 2;
                        }
                        break;
                    }
                case Key.Escape:
                    {
                        this.DialogResult = false;

                        this.Close();
                        break;
                    }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (SelectAllWhenLoaded)
            {
                this.inputTextBox.SelectAll();
            }
            else
            {
                this.inputTextBox.Select(this.inputTextBox.Text.Length, 0);
            }
        }

        private bool selectAllWhenLoaded = false;

        public bool SelectAllWhenLoaded
        {
            get { return selectAllWhenLoaded; }
        }

        private void inputTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            bool isCtrl = false;
            bool isShift = false;

            KeyboardDevice kd = e.KeyboardDevice;

            if ((kd.GetKeyStates(Key.LeftCtrl) & KeyStates.Down) > 0 ||
                (kd.GetKeyStates(Key.RightCtrl) & KeyStates.Down) > 0)
            {
                isCtrl = true;
            }

            if ((kd.GetKeyStates(Key.LeftShift) & KeyStates.Down) > 0 ||
                (kd.GetKeyStates(Key.RightShift) & KeyStates.Down) > 0)
            {
                isShift = true;
            }

            switch (e.Key)
            {
                case Key.V:
                    {
                        if (isCtrl && !isShift)
                        {
                            inputTextBox.SelectedText = Clipboard.GetText(TextDataFormat.UnicodeText);
                            inputTextBox.Select(inputTextBox.SelectionStart + inputTextBox.SelectionLength, 0);
                            e.Handled = true;
                        }
                        break;
                    }
            }
        }
    }
}
